//
//  WBNewFeatureView.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/17.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var enterBtn: UIButton!
    @IBOutlet weak var pageControl: UIPageControl!
    
    class func newFeatureView() -> WBNewFeatureView {
        
        let nib = UINib.init(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        v.backgroundColor = UIColor.clear
        
        return v
    }
    
    /// 进入微博
    @IBAction func enterStatus() {
        removeFromSuperview()
    }
    
    override func awakeFromNib() {
        debugPrint(bounds)
        
        for i in 0..<5 {
            let imageName = "new_feature_h_1"
            let iv = UIImageView(image: UIImage(named: imageName))
            iv.frame = UIScreen.main.bounds.offsetBy(dx: CGFloat(i) * IPHONE_WIDTH, dy: 0)
            
            if i != 4 {
                scrollView.addSubview(iv)
            }
        }
        
        scrollView.contentSize = CGSize(width: IPHONE_WIDTH * 5.0, height: IPHONE_HEIGHT)
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        enterBtn.isHidden = true
    }
}


extension WBNewFeatureView: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width) + 1
        
        if page == scrollView.subviews.count {removeFromSuperview()}
        
        enterBtn.isHidden = !(page == scrollView.subviews.count - 1)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        enterBtn.isHidden = true
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        pageControl.currentPage = page
    
        pageControl.isHidden = (page == scrollView.subviews.count - 1)
    }
}
